package org.springframework.boot.autoconfigure.klock.test.service;

import org.springframework.boot.autoconfigure.klock.annotation.Klock;
import org.springframework.boot.autoconfigure.klock.util.KlockUtil;
import org.springframework.stereotype.Service;

import static org.springframework.boot.autoconfigure.klock.model.LockTimeoutStrategy.KEEP_ACQUIRE;

/**
 * @Author ygm
 * @Date 2022/8/4
 */
@Service
public class InnerService {

    @Klock(name = "name", keys = {"#userId"},lockTimeoutStrategy = KEEP_ACQUIRE)
    public String secondLevel(String userId){
        System.out.println(Thread.currentThread().getId() + " - inner in - " + userId);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getId() + " - inner out - " + userId);
        return "second success ";
    }

    public String secondLevelUtil(String userId){
        KlockUtil.executeWithDefaultConfig("name",userId,() -> {
            System.out.println(Thread.currentThread().getId() + " - inner in - " + userId);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getId() + " - inner out - " + userId);
        });
        return "second success ";
    }
}
